package com.dq.domain.message.oneself;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dq.vo.baseVo.VO;
import com.dq.domain.message.oneself.centre.InstructionStaffuserCentre;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liuhui
 * @date 2019/11/22 0022 10:31:46
 */
@Data
@Entity
@ApiModel("指令消息")
public class InstructionMessage extends Message{

    @ApiModelProperty("内容")
    protected String content;
    @ApiModelProperty(value = "指令开始时间",hidden = true)
    @NotNull
    protected Long beginTimeRestrict;
    @ApiModelProperty(value = "指令结束时间",hidden = true)
    @NotNull
    protected Long nedTimeRestrict;

    @ApiModelProperty("附件地址")
    @Column(columnDefinition = "text")
    public String annexUrl;
    @Column(columnDefinition = "text")
    @ApiModelProperty("图片地址")
    public String imageUrl;
    @ApiModelProperty("分配员工id集合")
    @Transient
    protected List<Long> userIds = new ArrayList<>();


    @Data
    @ApiModel("视图")
    public static class Vo implements VO {
        protected Long id;
        @ApiModelProperty("主题名称")
        public String theme;
        @ApiModelProperty("内容")
        protected String content;
        @ApiModelProperty(value = "指令开始时间",hidden = true)
        protected Long beginTimeRestrict;
        @ApiModelProperty(value = "指令结束时间",hidden = true)
        protected Long nedTimeRestrict;

        @ApiModelProperty("附件地址")
        public String[] annexUrl;
        @ApiModelProperty("图片地址")
        public String[] imageUrl;
        @ApiModelProperty("创建时间")
        public String createTime;

        @ApiModelProperty("未读/已读")
        public String messageStatusEn;
        @ApiModelProperty("未读/已读")
        public String messageStatusCn;
        @ApiModelProperty("消息类型")
        public String messageTypeEn;
        @ApiModelProperty("消息类型")
        public String messageTypeCn;


    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.theme = this.theme;
        vo.content = this.content;
        vo.beginTimeRestrict = this.beginTimeRestrict;
        vo.nedTimeRestrict = this.nedTimeRestrict;

        if(StringUtils.isNotBlank(this.annexUrl)) {
            vo.annexUrl = this.annexUrl.split(",");
        }
        if(StringUtils.isNotBlank(this.imageUrl)) {
            vo.imageUrl = this.imageUrl.split(",");
        }

        vo.createTime = fmt(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);

        // 未读要根据中间表 判断
//        vo.messageStatusEn = this.getMessageStatus().toString();
//        vo.messageStatusCn = this.getMessageStatus().name;
        vo.messageTypeEn = this.getMessageType().toString();
        vo.messageTypeCn = this.getMessageType().name;

        return vo;
    }

    /**
     * 只返回自己回复的指令，       orgId无值， userId传值
     * 返回门店下所有员工的回复，    orgId传值， userId可有可无
     * 返回所有回复美容，            orgId无值， userId无值
     *
     * @param centres
     * @param userId 回复指令员工 id
     * @return
     */
    public JSONObject toJson(List<InstructionStaffuserCentre> centres, Long userId, Long orgId) {
        JSONObject vo = new JSONObject();
        vo.put("id", this.id);
        vo.put("theme", this.theme);
        vo.put("content", this.content);
        vo.put("beginTimeRestrict", fmt(this.beginTimeRestrict, TimeUtil.YYYY_MM_DD_HH_MM_SS));
        vo.put("nedTimeRestrict", fmt(this.nedTimeRestrict, TimeUtil.YYYY_MM_DD_HH_MM_SS));
        vo.put("annexUrl", fmt(this.annexUrl));
        vo.put("imageUrl", fmt(this.imageUrl));
        vo.put("createTime", fmt(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS));

        vo.put("messageTypeEn", this.getMessageType().toString());
        vo.put("messageTypeCn", this.getMessageType().name);

        // 指派给员工的集合
        JSONArray userArray = new JSONArray();
        // 回复指令的员工的集合
        JSONArray returnArray = new JSONArray();
        if(centres != null && centres.size() > 0) {
            // 获取 指令下回复指令列表 / 指令指派人列表
            for (int i = 0; i < centres.size(); i++) {
                InstructionStaffuserCentre centre = centres.get(i);
                if(centre != null ) {
                    if(orgId != null) {
                        if(orgId.toString().equals(centre.getOrg().getId().toString())) {
                            if(centre.getMessageStatus().toString().equals(MessageStatus.noSee.toString())) {
                                vo.put("messageStatusEn", MessageStatus.noSee.toString());
                                vo.put("messageStatusCn", MessageStatus.noSee.name);
                            }
                        }
                        if(i == centres.size()-1) {
                            if(vo.get("messageStatusEn") == null) {
                                vo.put("messageStatusEn", MessageStatus.alreadySee.toString());
                                vo.put("messageStatusCn", MessageStatus.alreadySee.name);
                            }
                        }

                        if(centre.getInstructionMessageReturn() != null) {
                            if(orgId.toString().equals(centre.getInstructionMessageReturn().getCreateUser().getSection().getId().toString())) {
                                returnArray.add(getReturnInfo(centre));
                            }
                        }
                    }else {
                        if(userId != null) {
                            if(userId.toString().equals(centre.staffUser.getId().toString())) {
                                vo.put("messageStatusEn", centre.getMessageStatus().toString());
                                vo.put("messageStatusCn", centre.getMessageStatus().name);
                            }

                            if(centre.getInstructionMessageReturn() != null) {
                                if(userId.toString().equals(centre.getInstructionMessageReturn().getCreateUser().getId().toString())) {
                                    returnArray.add(getReturnInfo(centre));
                                }
                            }
                        } else {
                            vo.put("messageStatusEn", this.getMessageStatus().toString());
                            vo.put("messageStatusCn", this.getMessageStatus().name);

                            if(centre.getInstructionMessageReturn() != null) {
                                returnArray.add(getReturnInfo(centre));
                            }
                        }
                    }

                    if(centre.getStaffUser().getId() != null) {
                        JSONObject userInfo = new JSONObject();
                        userInfo.put("userId", centre.getStaffUser().getId());
                        userInfo.put("userName", centre.getStaffUser().getUserRealcName());
                        userArray.add(userInfo);
                    }
                }
            }
        }
        vo.put("userArray", userArray);
        vo.put("returnArray", returnArray);

        return vo;
    }

    public JSONObject getReturnInfo(InstructionStaffuserCentre centre) {
        JSONObject returnInfo = new JSONObject();
        returnInfo.put("id", centre.getInstructionMessageReturn().getId());
        returnInfo.put("annexUrl", centre.getInstructionMessageReturn().getAnnexUrl());
        returnInfo.put("imageUrl", centre.getInstructionMessageReturn().getImageUrl());
        returnInfo.put("remark", centre.getInstructionMessageReturn().getRemark());
        returnInfo.put("userId", centre.getStaffUser().getId());
        returnInfo.put("userName", centre.getStaffUser().getUserRealcName());
        returnInfo.put("status", centre.getInstructionMessageReturn().getStatus());
        returnInfo.put("returnId",centre.getInstructionMessageReturn().getId());
        returnInfo.put("bossReturnStatus",centre.getInstructionMessageReturn().getBossStatus());
        return returnInfo;
    }

}
